import scrapy

from doubanmovie.items import DoubanmovieItem


class MoviespiderSpider(scrapy.Spider):
    name = 'moviespider'
    allowed_domains = ['movie.douban.com']
    start_urls = ['https://movie.douban.com/top250']

    def start_requests(self):
        for i in range(10):
            url = f'https://movie.douban.com/top250?start={i * 25}'
            yield scrapy.Request(url, callback=self.parse)
        pass

    def parse(self, response):
        sel = scrapy.Selector(response)
        list_items = sel.css('#content > div > div.article > ol > li')
        for list_item in list_items:
            doubanmovie_item = DoubanmovieItem()
            doubanmovie_item['title'] = list_item.css(
                'span.title::text').extract_first()
            doubanmovie_item['rank'] = list_item.css(
                'span.rating_num::text').extract_first()
            doubanmovie_item['subject'] = list_item.css(
                'span.inq::text').extract_first()
            yield doubanmovie_item
        pass
        # hrefs_list = sel.css('div.paginator > a ::attr(href)').extract()
        # for href in hrefs_list:
        #     next_url = response.urljoin(href)
        #     yield scrapy.Request(next_url, callback=self.parse)
        # pass
       
